$(document).ready(function () {
  var hulla = new hullabaloo()
  var infoId = 0
  var data = {}
  var centerId;
  /**
   * @description: 预加载
   * @param {*}
   * @return {*}
   */
  $.ajax({
    url: '../../center/getListCenter', //引入地址
    type: 'GET', //请求方式
    success: function (response) {
      // response = {
      //   data: [{ id: 1, infoContent: 'aaaa', infoTime: '23:22', infoType: 'A' }]
      // }
      var html = ''
      data = response.data.list
      for (var i = 0; i < data.length; i++) {
          html += `<tr class="infoTableClass"><td>${data[i].centerId}</td> 
            <td>${data[i].centerDetail}</td> 
            <td>${data[i].centerTime}</td>
            <td>${data[i].centerType}</td>
            <td>
            <button id='${data[i].centerId}' onclick = 'deleteRow(this)' type="button" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
            </button> 
            <button   data-toggle="modal" data-target="#replaceInfoModal"  onclick = 'changeRow(this)' id='${data[i].centerId}' type="button" class="btn btn-info delete">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
            </button>
            <button   data-toggle="modal" data-target="#sendInfoModal"  onclick = 'sendRow(this)' id='${data[i].centerId}' type="button" class="btn btn-success">
            <span class="glyphicon glyphicon-send" aria-hidden="true"></span>
            </button>
            </td></tr>`

      }
      $('#infoTableT').append(html)
    },
    error: function (error) {
      hulla.send(error.message, 'danger')
    }
  })

  /**
   * @description: 添加信息
   * @param {*}
   * @return {*}
   */
  addInfoBtn = function () {
    var inforDetail = $('#inputInfoContent').val()
    // var inforTime = $('#inputInfoTime').val()
    var inforType = $('#inputInfoType').val()
    if (inforDetail == '') {
      $('#infoContentForm').addClass('has-error')
      $('#infoContentIptDiv').append(
        " <span class='help-block'>不可为空</span>"
      )
      setTimeout(() => {
        $('#infoContentForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    // if (inforTime == '') {
    //   $('#infoTimeForm').addClass('has-error')
    //   $('#infoTimeIptDiv').append(" <span class='help-block'>不可为空</span>")
    //   setTimeout(() => {
    //     $('#infoTimeForm').removeClass('has-error')
    //     $('.help-block').remove()
    //   }, 3000)
    // }
    if (inforType == '') {
      $('#infoTypeForm').addClass('has-error')
      $('#infoTypeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#infoTypeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (inforDetail != '' && inforType != '') {
      $.ajax({
        url: '../../center/addCenter', //引入地址
        type: 'POST', //请求方式
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify({              //JSON.stringify保证传过去的数据是json格式
                                                  //
          "centerDetail": inforDetail,
          // "centerTime": inforTime,
          "centerType": inforType
        }),
        success: function (response) {
          $('.help-block').remove()
          if (response.success) {
            hulla.send(response.message, 'success')
            window.location.reload()
          }
        },
        error: function (response) {
          hulla.send(response.message, 'danger')
        }
      })
    }
  }

  /**
   * @description: 删除信息
   * @param {*}
   * @return {*}
   */
  deleteRow = function (tr) {
    $.ajax({
      url: '../../center/deleteCenter/'+ tr.id,
      type: 'GET',
      data: {
      },
      success: function (response) {

        hulla.send(response.message, 'success')
        window.location.reload()
      },
      error: function (error) {
        hulla.send(response.message, 'danger')
      }
    })
  }

  /**
   * @description: 发送信息给RSU
   * @param {*}
   * @return {*}
   */
  sendRow = function (tr) {
    centerId = tr.id;

  }

  sendThisRow = function(){
    var sendRSUId = $('#sendRSUId').val()
    var strategy = $('#strategy').val()
    // var infoTitle = $('#infoTitle').val()

    $.ajax({
      url: '../../center/sendMsg',
      type: 'POST',
      dataType: "json",
      contentType: "application/json;charset=UTF-8",
      data: JSON.stringify({
        centerId:centerId,
        rsuId:sendRSUId,
        msgStrategy:strategy,
        // title:infoTitle
      }),
      success: function (response) {
        hulla.send(response.message, 'success')
        window.location.reload()
      },
      error: function (error) {
        hulla.send(response.message, 'danger')
      }
    })
  }

  /**
   * @description: 修改信息
   * @param {*}
   * @return {*}
   */
  changeRow = function (tr) {
    infoId = tr.id
    let thisRow = [];
    for(let i = 0;i<data.length;i++){
      if(data[i].centerId == tr.id)
        thisRow = data[i]
    }
    // $("#inputRepInfoTime").attr("value",data[i].centerTime);
    $("#inputRepInfoContent").val(thisRow.centerDetail)
    $("#inputRepInfoTime").val(thisRow.centerTime)
    $("#inputRepInfoType").val(thisRow.centerType)
    $("#inputRepId").val(thisRow.centerId)
    $.ajax({
      url: '', //引入地址
      type: 'GET', //请求方式
      success: function (response) {
        // response = {
        //   data: [
        //     { id: 1, infoContent: 'aaaa', infoTime: '23:22', infoType: 'A' }
        //   ]
        // }
        // $('#inputInfoContent').val(response.data[0].infoContent)
        // $('#inputInfoTime').val(response.data[0].infoTime)
        // $('#inputInfoType').val(response.data[0].infoType)
      },
      error: function (error) {
        //请求失败的回调方法
        //   var html = "";
        //   html += "<tr><td>数据请求失败</td></tr>";
        //   $('#infoTable').html(html)

      }
    })
  }

  /**
   * @description: 提交修改
   * @param {*}
   * @return {*}
   */
  replaceInfoBtn = function () {
    var inforDetail = $('#inputRepInfoContent').val()
    var inforId = $('#inputRepId').val()
    var inforTime = $('#inputRepInfoTime').val()
    var inforType = $('#inputRepInfoType').val()
    if (inforDetail == '') {
      $('#infoRepContentForm').addClass('has-error')
      $('#infoRepContentIptDiv').append(
        " <span class='help-block'>不可为空</span>"
      )
      setTimeout(() => {
        $('#infoRepContentForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (inforTime == '') {
      $('#infoRepTimeForm').addClass('has-error')
      $('#infoRepTimeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#infoRepTimeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (inforType == '') {
      $('#infoRepTypeForm').addClass('has-error')
      $('#infoRepTypeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#infoRepTypeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (inforDetail != '' && inforTime != '' && inforType != '') {
      $.ajax({
        url: '../../center/updateCenter', //引入地址
        type: 'POST', //请求方式
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify( {
          centerId:inforId,
          centerDetail: inforDetail,
          centerTime: inforTime,
          centerType: inforType
        }),
        success: function (response) {
          $('.help-block').remove()
          if (response.success) {
            hulla.send(response.message, 'success')
            window.location.reload()
          }
        },
        error: function (response) {
          hulla.send(response.message, 'danger')
        }
      })
    }
  }
})

